{
  Nassy - anti-spam for IM clients
  Copyright (C) 2008-2009 Denis Shemanayev aka nis
                          mailto: nis@nis.org.ua
                          mailto: Denis.Shemanayev@gmail.com

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
}
{ $Id: uOptionsForm.pas 5 2009-06-19 12:15:33Z Denis.Shemanayev $ }

unit uOptionsForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, TntForms, uMainForm, StdCtrls, TntStdCtrls, Buttons, TntButtons,
  ComCtrls, TntComCtrls, Menus, TntMenus, Grids, TntGrids;

const
  C_SVNRevision = '$Revision: 4 $';

type
  TfrmNassyOptions = class(TfrmMain)
    PageMain: TTntPageControl;
    TabMain: TTntTabSheet;
    TabAbout: TTntTabSheet;
    BtnSave: TTntBitBtn;
    BtnCancel: TTntBitBtn;
    BtnWWW: TTntBitBtn;
    BtnShowLog: TTntBitBtn;
    MnuPopupTmpl: TTntPopupMenu;
    MnuItemnick1: TTntMenuItem;
    MnuItemacc1: TTntMenuItem;
    MnuItemproto1: TTntMenuItem;
    grp1: TTntGroupBox;
    CBLog: TTntCheckBox;
    CBContact: TTntCheckBox;
    CBAvatarBtn: TTntCheckBox;
    EdAnswerText: TTntEdit;
    LblReplaceMsg: TTntLabel;
    BtnPopupTempl: TTntBitBtn;
    CBPopupShow: TTntCheckBox;
    CBAnswerSend: TTntCheckBox;
    TabFiltering: TTntTabSheet;
    grp2: TTntGroupBox;
    TrbThreshold: TTntTrackBar;
    Lbl1: TTntLabel;
    EdPopupMsg: TTntMemo;
    grp3: TTntGroupBox;
    MemAbout: TTntMemo;
    LblTranslator: TTntLabel;
    Lbl2: TTntLabel;
    Lbl4: TTntLabel;
    EdAnswerMsg: TTntMemo;
    CBFilterIdent: TTntCheckBox;
    SGWhiteList: TTntStringGrid;
    SGBlackList: TTntStringGrid;
    BtnBlackAdd: TTntBitBtn;
    BtnBlackDelete: TTntBitBtn;
    BtnWhiteAdd: TTntBitBtn;
    BtnWhiteDelete: TTntBitBtn;
    Lbl3: TTntLabel;
    Lbl5: TTntLabel;
    LblRevision: TTntLabel;
    procedure TntFormCreate(Sender: TObject);
    procedure BtnWWWClick(Sender: TObject);
    procedure BtnCancelClick(Sender: TObject);
    procedure BtnSaveClick(Sender: TObject);
    procedure CBAnswerSendClick(Sender: TObject);
    procedure BtnPopupTemplClick(Sender: TObject);
    procedure MnuItemnick1Click(Sender: TObject);
    procedure BtnShowLogClick(Sender: TObject);
    procedure CBFilterIdentClick(Sender: TObject);
    procedure CBPopupShowClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure BtnWhiteAddClick(Sender: TObject);
    procedure BtnWhiteDeleteClick(Sender: TObject);
    procedure BtnBlackAddClick(Sender: TObject);
    procedure BtnBlackDeleteClick(Sender: TObject);
  private
    procedure LoadSettings;
    procedure SaveSettings;
  public
    { Public declarations }
  end;

var
  frmOptions: TfrmNassyOptions;

implementation

{$R *.DFM}

uses
  uLogForm, TntWindows, uConst, gnugettext, uQipPlugin, uGrahamAlgorithm,
  TntSysUtils, StrUtils;

{***********************************************************}
procedure TfrmNassyOptions.TntFormCreate(Sender: TObject);
var
  i: Integer;
begin
  frmOptions := Self;
  Plugin.ILButtons.GetIcon(12, Icon);
  Plugin.ILButtons.GetBitmap(0, BtnSave.Glyph);
  Plugin.ILButtons.GetBitmap(1, BtnCancel.Glyph);
  Plugin.ILButtons.GetBitmap(3, BtnWWW.Glyph);
  Plugin.ILButtons.GetBitmap(5, BtnWhiteDelete.Glyph);
  Plugin.ILButtons.GetBitmap(6, BtnWhiteAdd.Glyph);
  Plugin.ILButtons.GetBitmap(5, BtnBlackDelete.Glyph);
  Plugin.ILButtons.GetBitmap(6, BtnBlackAdd.Glyph);
  Plugin.ILMenus.GetBitmap(0, BtnShowLog.Glyph);
 // TntPageWhiteList.ActivePageIndex := 0;
 // PageBlacklist.ActivePageIndex := 0;
 { EdWhiteUser.Text := '';
  EdWhiteText.Text := '';
  EdBlackUser.Text := '';
  EdBlackText.Text := '';  }
  TP_Ignore(Self, 'MemAbout.Lines.Text');
  TP_Ignore(Self, 'EdPopupMsg.Text');
  TP_Ignore(Self, 'EdAnswerMsg.Text');
  TP_Ignore(Self, 'EdAnswerText.Text');
  PageMain.ActivePageIndex := 0;
  EdAnswerMsg.Enabled := False;
  EdAnswerText.Enabled := False;
  EdPopupMsg.Enabled := False;
  LblTranslator.Caption := _('Translated by:') + WideString(' ') + GetTranslatorNameAndEmail;
  LoadSettings;
  i := Pos(' ', C_SVNRevision);
  LblRevision.Caption := 'SVN: ' + Copy(C_SVNRevision, i + 1, Length(C_SVNRevision) -
   PosEx(' ', C_SVNRevision, i + 1));
end;

{***********************************************************}
procedure TfrmNassyOptions.BtnWWWClick(Sender: TObject);
begin
  Tnt_ShellExecuteW(0, nil, C_LINK_HOME, nil, nil, SW_SHOW);
end;

{***********************************************************}
procedure TfrmNassyOptions.LoadSettings;
const
  Tresh: array[1..5] of Extended = (0.9, 0.8, 0.7, 0.6, 0.5);
var
  i: Integer;
  E: Extended;
begin
  CBPopupShow.Checked := Plugin.Options.GetBool('popup_show', True);
  CBAnswerSend.Checked := Plugin.Options.GetBool('answer_send', False);
  CBContact.Checked := Plugin.Options.GetBool('contact_show', True);
  CBAvatarBtn.Checked := Plugin.Options.GetBool('avatar_btn', True);
  CBLog.Checked := Plugin.Options.GetBool('log_enable', True);
  CBFilterIdent.Checked := Plugin.Options.GetBool('filter_identical', False);

  EdPopupMsg.Text := Plugin.Options.GetString('popup_msg',
   _('Message from %nick% ("%acc%" - %proto%) was blocked.'));
  EdAnswerMsg.Text := Plugin.Options.GetString('answer_msg',
   _('Your message blocked by anti-spam. Please don''t spam!'));
  EdAnswerText.Text := Plugin.Options.GetString('answer_text',
   _('*** There was SPAM ***'));

  E := Plugin.Options.GetFloat('threshold', C_DEFAULT_SPAM_THRESHOLD);
  for i := 1 to 5 do
    if Tresh[i] = e then
      TrbThreshold.Position := i;

  if CBAnswerSend.Checked then
  begin
    EdAnswerMsg.Enabled := True;
    EdAnswerText.Enabled := True;
  end;
  if CBPopupShow.Checked then
    EdPopupMsg.Enabled := True;
  SGWhiteList.Cols[0].Clear;
  SGBlackList.Cols[0].Clear;
  //SGWhiteList.Cols[0].AddStrings(Plugin.WhiteList);
  //SGBlackList.Cols[0].AddStrings(Plugin.BlackList);
  SGWhiteList.RowCount := Plugin.WhiteList.Count;
  SGBlackList.RowCount := Plugin.BlackList.Count;
  for i := 0 to Plugin.WhiteList.Count - 1 do
    SGWhiteList.Cells[0, i] := Plugin.WhiteList.Strings[i];
  for i := 0 to Plugin.BlackList.Count - 1 do
    SGBlackList.Cells[0, i] := Plugin.BlackList.Strings[i];
end;

{***********************************************************}
procedure TfrmNassyOptions.SaveSettings;
var
  E: Extended;
  i: Integer;
begin
  Plugin.Options.SetBool('popup_show', CBPopupShow.Checked);
  Plugin.Options.SetBool('answer_send', CBAnswerSend.Checked);
  Plugin.Options.SetBool('contact_show', CBContact.Checked);
  Plugin.Options.SetBool('avatar_btn', CBAvatarBtn.Checked);
  Plugin.Options.SetBool('log_enable', CBLog.Checked);
  Plugin.Options.SetBool('filter_identical', CBFilterIdent.Checked);

  Plugin.Options.SetString('popup_msg', Tnt_WideStringReplace(EdPopupMsg.Text, CRLF, '', [rfReplaceAll]));
  Plugin.Options.SetString('answer_msg',Tnt_WideStringReplace(EdAnswerMsg.Text, CRLF, '', [rfReplaceAll]));
  Plugin.Options.SetString('answer_text', EdAnswerText.Text);

  case TrbThreshold.Position of
    5: E := 0.5;
    4: E := 0.6;
    3: E := 0.7;
    2: E := 0.8;
    1: E := 0.9;
    else
      E := 0.9;
  end;
  Plugin.Options.SetFloat('threshold', E);

  Plugin.WhiteList.Clear;
  Plugin.BlackList.Clear;
  //Plugin.WhiteList.AddStrings(SGWhiteList.Cols[0]);
  //Plugin.BlackList.AddStrings(SGBlackList.Cols[0]);
  for i := 0 to SGWhiteList.RowCount - 1 do
    if Trim(SGWhiteList.Cells[0, i]) <> '' then
      Plugin.WhiteList.Add(SGWhiteList.Cells[0, i]);
  for i := 0 to SGBlackList.RowCount - 1 do
    if Trim(SGBlackList.Cells[0, i]) <> '' then
      Plugin.BlackList.Add(SGBlackList.Cells[0, i]);

  Plugin.SavePluginOptions;
end;

{***********************************************************}
procedure TfrmNassyOptions.BtnCancelClick(Sender: TObject);
begin
  Close;
end;

{***********************************************************}
procedure TfrmNassyOptions.BtnSaveClick(Sender: TObject);
begin
  SaveSettings;
  Plugin.Classifier.SpamThreshold := Plugin.Options.GetFloat('threshold',
   C_DEFAULT_SPAM_THRESHOLD);
  if CBContact.Checked then
    Plugin.ContactAdd
  else
    Plugin.ContactDelete;
  Close;
end;

{***********************************************************}
procedure TfrmNassyOptions.CBAnswerSendClick(Sender: TObject);
begin
  EdAnswerMsg.Enabled := CBAnswerSend.Checked;
  EdAnswerText.Enabled := CBAnswerSend.Checked;
  Plugin.MnuContact.Items[2].Checked := CBAnswerSend.Checked;
end;

{***********************************************************}
procedure TfrmNassyOptions.BtnPopupTemplClick(Sender: TObject);
var
  Pos: TPoint;
begin
  GetCursorPos(Pos);
  MnuPopupTmpl.Popup(Pos.X, Pos.Y);
end;

{***********************************************************}
procedure TfrmNassyOptions.MnuItemnick1Click(Sender: TObject);
begin
  EdPopupMsg.SelText := (Sender as TTntMenuItem).Hint;
end;

{***********************************************************}
procedure TfrmNassyOptions.BtnShowLogClick(Sender: TObject);
begin
  Plugin.ShowLog;
end;

{***********************************************************}
procedure TfrmNassyOptions.CBFilterIdentClick(Sender: TObject);
begin
  Plugin.MnuContact.Items[3].Checked := CBFilterIdent.Checked;
  Plugin.MnuContact.Items[1].Checked := CBFilterIdent.Checked;
end;

{***********************************************************}
procedure TfrmNassyOptions.CBPopupShowClick(Sender: TObject);
begin
  EdPopupMsg.Enabled := CBPopupShow.Checked;
  Plugin.MnuContact.Items[1].Checked := CBPopupShow.Checked;
end;

{***********************************************************}
procedure TfrmNassyOptions.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:= caFree;
  frmOptions := nil;
end;

{***********************************************************}
procedure TfrmNassyOptions.BtnWhiteAddClick(Sender: TObject);
begin
  SGWhiteList.RowCount := SGWhiteList.RowCount + 1;
  SGWhiteList.Row := SGWhiteList.RowCount - 1;
end;

{***********************************************************}
procedure TfrmNassyOptions.BtnWhiteDeleteClick(Sender: TObject);
var
  i: Integer;
begin
  for i := SGWhiteList.Row to SGWhiteList.RowCount - 1 do
    SGWhiteList.Cells[0, i] := SGWhiteList.Cells[0, i + 1];
  SGWhiteList.RowCount := SGWhiteList.RowCount - 1;
end;

{***********************************************************}
procedure TfrmNassyOptions.BtnBlackAddClick(Sender: TObject);
begin
  SGBlackList.RowCount := SGBlackList.RowCount + 1;
  SGBlackList.Row := SGBlackList.RowCount - 1;
end;

{***********************************************************}
procedure TfrmNassyOptions.BtnBlackDeleteClick(Sender: TObject);
var
  i: Integer;
begin
  for i := SGBlackList.Row to SGBlackList.RowCount - 1 do
    SGBlackList.Cells[0, i] := SGBlackList.Cells[0, i + 1];
  SGBlackList.RowCount := SGBlackList.RowCount - 1;
end;

end.
